package com.lzy.cloud.base.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.base.dto.BaseCouponDto;
import com.lzy.cloud.base.facade.CouponFacade;
import com.lzy.cloud.base.request.BaseCouponRequest;
import com.lzy.cloud.base.response.BaseCouponResponse;
import com.lzy.cloud.base.service.CouponService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 卡券
 */
@RestController
@RequestMapping(value = "/coupon")
@Slf4j
public class CouponController {

    @Autowired
    private CouponService couponService;
    @Autowired
    private CouponFacade couponFacade;

    /**
     * 查询卡券列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Result queryList(@RequestBody BaseCouponRequest request) {
        Ensure.that(request).isNotNull("00000002");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<BaseCouponDto> pageInfo = couponService.queryList(pager,DozerUtils.map(request,BaseCouponDto.class));
        if (pageInfo != null && pageInfo.getList() != null) {
            pager.setTotal(pageInfo.getTotal());
            return new Result(pager,DozerUtils.maps(pageInfo.getList(), BaseCouponResponse.class));
        } else {
            return new Result();
        }
    }

    /**
     * 新增卡券
     * @param request
     * @return
     */
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public Result create(BaseCouponRequest request) {
        Ensure.that(request).isNotNull("00000002");
        //        UserToken userToken = isLogin(token);
//        request.setUpdateUser(userToken.getId());
        return new Result(couponFacade.insert(DozerUtils.map(request, BaseCouponDto.class)));
    }

    /**
     * 更新卡券
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody BaseCouponRequest request) {
        Ensure.that(request).isNotNull("00000002");
//        UserToken userToken = isLogin(token);
//        request.setUpdateUser(userToken.getId());
        Ensure.that(couponService.update(DozerUtils.map(request,BaseCouponDto.class))).isTrue("00040002");
        return new Result();
    }

    /**
     * 更新卡券
     * @param request
     * @return
     */
    @RequestMapping(value = "/update/by/primary/key", method = RequestMethod.POST)
    public Result updateByKey(BaseCouponRequest request) {
        Ensure.that(couponService.update(DozerUtils.map(request,BaseCouponDto.class))).isTrue("00040002");
        return new Result();
    }

    /**
     * 卡券详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public Result detail(@PathVariable("id") Long id) {
        BaseCouponDto couponDto = couponService.find(id);
        return new Result(DozerUtils.map(couponDto, BaseCouponResponse.class));
    }

    /**
     * 根据id删除卡券，假删
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Result delete(@PathVariable("id") Long id) {
        Ensure.that(couponService.deleteById(id)).isTrue("00040005");
        return new Result();
    }

    /**
     * 房车租赁，选择卡券
     * @param ids
     * @return
     */
    @RequestMapping(value = "/find/car/rent/by/ids", method = RequestMethod.POST)
    public Result findCarRentByIds(@RequestBody List<Long> ids) {
        List<BaseCouponDto> list = couponService.findCarRentByIds(ids);
        return new Result(DozerUtils.maps(list, BaseCouponResponse.class));
    }

    /**
     * 定时任务：卡券（房车券，房车代金券)状态修改为已过期
     * @return
     */
    @RequestMapping(value = "/update/status/expired", method = RequestMethod.POST)
    public Result updateExpiredStatus(){
        return new Result(couponFacade.updateExpiredStatus());
    }

    /**
     * 定时任务：
     * 执行券状态变更任务（即将过期状态的更新）（提前三个月）
     * 0 0 2 * * ?---------------在每天凌晨2：00触发
     *
     * @return
     */
    @RequestMapping(value = "/update/status/expiring/three",method = RequestMethod.POST)
    public void updateExpiringStatusThree() {
        couponFacade.updateExpiringStatusThree();
    }
    /**
     * 定时任务：
     * 执行券状态变更任务（即将过期状态的更新）（提前两个月）
     * 0 0 2 * * ?---------------在每天凌晨2：00触发
     *
     * @return
     */
    @RequestMapping(value = "/update/status/expiring/two",method = RequestMethod.POST)
    public void updateExpiringStatusTwo(){
        couponFacade.updateExpiringStatusTwo();
    }
    /**
     * 定时任务：
     * 执行券状态变更任务（即将过期状态的更新）（提前一个月）
     * 0 0 2 * * ?---------------在每天凌晨2：00触发
     *
     * @return
     */
    @RequestMapping(value = "/update/status/expiring/one",method = RequestMethod.POST)
    public void updateExpiringStatusOne(){
        updateExpiringStatusOne();
    }
}
